//
//  HMHomeVC.swift
//  swift一遍敲
//
//  Created by 刘耀宗 on 16/4/19.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

import UIKit

class HMHomeVC: HMBaseTableViewVC {
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavigation()
    }
    
   private func setNavigation()
    {
    
       navigationItem.leftBarButtonItem = makeBtn("navigationbar_friendsearch", highImageName: "navigationbar_friendsearch_highlighted")
        navigationItem.rightBarButtonItem = makeBtn("navigationbar_pop", highImageName: "navigationbar_pop_highlighted")
        
        self.navigationItem.titleView = titleBtn
    
    
    }
    private func titleView() -> UIButton
    {
        let btn = UIButton()
        let accessAcount = HMUserAccessModel.shareAccessModel.userAccess?.screen_name
        btn.setTitle(accessAcount, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        btn.sizeToFit()
    
       
        return btn
    
    
    
    }
    @objc private func clickBtn()
    {
        print("--------")
        titleBtn.selected = !titleBtn.selected
        if titleBtn.selected {
            UIView.animateWithDuration(0.25, animations: { 
                 self.titleBtn.imageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI) - 0.01)
            })
           
        }
        else
        {
             UIView.animateWithDuration(0.25, animations: { 
                 self.titleBtn.imageView?.transform = CGAffineTransformIdentity
             })
           
        
        }
      
        
    
    
    
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        titleView().titleLabel?.frame.origin.x = 0
        titleView().imageView?.frame.origin.x = (titleView().titleLabel?.frame.size.width)!
        
    }
    private func makeBtn(normalImageName: String,highImageName: String) -> UIBarButtonItem
    {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: normalImageName), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: highImageName), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        return UIBarButtonItem(customView: btn)
    
    
    }
    //懒加载
    private lazy var titleBtn: HMBtn = {
        let btn = HMBtn()
        //注册单击事件
        btn.addTarget(self, action: #selector(HMHomeVC.clickBtn), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    
    
    }()
}
